#include "Logger.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
/**
 * @Note: Comment is encoded by utf-8.
 *
 * 1. 感谢<赖思敏>同学为测试程序填加的中文注释。
 */

//mian模块继承自root模块,root模块是根模块,本程序其他类都继承自mian模块
//前面相同,那个 . 就表示继承关系-_-!
//配置文件中mian=TRACE  因此main.test=RINFO|DEBUG + mian=TRACE
Logger *l_main = LogManager::instance ()->get_logger ("main");

//下面的main.*类都继承上面的mian模块
Logger *long_str = LogManager::instance ()->get_logger ("main.longstr");
Logger *l_sub1 = LogManager::instance ()->get_logger ("main.test.sub1");
Logger *l_sub2 = LogManager::instance ()->get_logger ("main.test.sub2");
Logger *l_sub3 = LogManager::instance ()->get_logger ("main.test.sub3");
Logger *l_sub4 = LogManager::instance ()->get_logger ("main.test.sub4");
Logger *l_sub5 = LogManager::instance ()->get_logger ("main.test.sub5");
Logger *l_sub6 = LogManager::instance ()->get_logger ("main.test.sub6");
Logger *l_sub7 = LogManager::instance ()->get_logger ("main.test.sub7");
Logger *l_sub8 = LogManager::instance ()->get_logger ("main.test.sub8");
Logger *l_sub9 = LogManager::instance ()->get_logger ("main.test.sub9");
Logger *l_sub10 = LogManager::instance ()->get_logger ("main.test.sub10");
Logger *l_sub11 = LogManager::instance ()->get_logger ("main.test.sub11");
Logger *l_sub12 = LogManager::instance ()->get_logger ("main.test.sub12");

void method_trace_1 (void);
void method_trace_2 (void);
void method_trace_3 (void);
void method_trace_4 (void);
void method_trace_5 (void);

void method_trace (void)
{
    //函数数据记录MethodTrace类,Logger.h中定义
    METHOD_TRACE (l_main);
    //为了便于观察日志文件中的记录,屏蔽下面的嵌套函数.
//    method_trace_1 ();
	printf("Funtion is over!!!!!!!!!!!!!!!!!\n");
}
void method_trace_1 (void)
{
    METHOD_TRACE (l_main);
    method_trace_2 ();
}
void method_trace_2 (void)
{
    METHOD_TRACE (l_main);
    method_trace_3 ();
    method_trace_3 ();
}
void method_trace_3 (void)
{
    METHOD_TRACE (l_main);
    method_trace_4 ();
}
void method_trace_4 (void)
{
    METHOD_TRACE (l_main);
    method_trace_5 ();
}
void method_trace_5 (void)
{
    METHOD_TRACE (l_main);
}

int main ()
{
    //读取配置文件
    if (LogManager::instance ()->init ("logger_config.ini") != 0)
    {
        fprintf (stderr, "Init failed\n");
        return -1;
    }
    //初始化l对象,属性mian.test继承mian的属性(前面相同,那个 . 就表示继承关系-_-!)
    Logger *l = LogManager::instance ()->get_logger ("main.test");
    if (l == 0) 
    {
        fprintf (stderr, "Get logger failed\n");
        return -1;
    }

    //初始化函数追踪log写入日志文件的字符串
    char longstr[1024] = {0};
    for (int i = 0; i < 1023; ++i)
    {
        longstr[i] = 'a' + i % 26;
    }
    //函数追踪log调用
    printf("\n\n\nfunction: l_main==main\n");
    method_trace ();

    //默认root模块属性全部屏蔽,因此不写人日志文件
    Logger *root_l = LogManager::instance ()->get_logger ("root");
    root_l->log (LL_FATAL, "hello root logger : fatal");
    root_l->log (LL_DEBUG, "hello root logger : debug");
    //下面这行没实际意义
    Logger *root_ll = LogManager::instance ()->get_logger ("root");

    int count = 0;
    //对象sub_l
    Logger *sub_l = 0;
    while (1)
    {
        //sub_l对象没有初始化,因此不会写日志文件
        if (sub_l)
        {
            sub_l->log (LL_TRACE, "hello logger ----- %s", "aaaaaa");
            sub_l->log (LL_TRACE, "hello logger trace %s", "aaaaaa");
            sub_l->log (LL_DEBUG, "hello logger debug %s", "aaaaaa");
            sub_l->log (LL_WNING, "hello logger wning %s", "aaaaaa");
            sub_l->log (LL_RINFO, "hello logger rinfo %s", "aaaaaa");
            sub_l->log (LL_FATAL, "hello logger fatal %s", "aaaaaa");
            usleep (100*1000);
        }

        //count的值>10才会初始化sub_l对象,下面设置count的值
        count = 11;
        if (count++ > 10 && sub_l == 0)
        {
            //读取main.test.sub模块,继承自mian.test模块
            sub_l = LogManager::instance ()->get_logger ("main.test.sub");
        }
        if (sub_l)
        {
            //sub_l已初始化,因此写日志文件
            sub_l->log (LL_TRACE, "hello logger ----- %s", "bbbbbb");
            sub_l->log (LL_TRACE, "hello logger trace %s", "bbbbbb");
            sub_l->log (LL_DEBUG, "hello logger debug %s", "bbbbbb");
            sub_l->log (LL_WNING, "hello logger wning %s", "bbbbbb");
            sub_l->log (LL_RINFO, "hello logger rinfo %s", "bbbbbb");
            sub_l->log (LL_FATAL, "hello logger fatal %s", "bbbbbb");
            usleep (100*1000);
        }
        //如果count的值>20则关闭此日志
        count = 21;
        if (count > 20 && sub_l)
        {
            sub_l->shutdown (true);
        }
        if (sub_l)
        {
            //sub_l已关闭,因此不会写日志文件
            sub_l->log (LL_TRACE, "hello logger ----- %s", "cccccc");
            sub_l->log (LL_TRACE, "hello logger trace %s", "cccccc");
            sub_l->log (LL_DEBUG, "hello logger debug %s", "cccccc");
            sub_l->log (LL_WNING, "hello logger wning %s", "cccccc");
            sub_l->log (LL_RINFO, "hello logger rinfo %s", "cccccc");
            sub_l->log (LL_FATAL, "hello logger fatal %s", "cccccc");
            usleep (100*1000);
        }



        //mian.test属性继承自main模块(前面相同,那个 . 就表示继承关系-_-!)
        //mian=TRACE  因此main.test=RINFO|DEBUG + mian=TRACE
        l->log (LL_TRACE, "hello logger ----- %d", 222222);
        l->log (LL_TRACE, "hello logger trace %d", 222222);
        l->log (LL_DEBUG, "hello logger debug %d", 222222);
        l->log (LL_WNING, "hello logger wning %d", 222222);
        l->log (LL_RINFO, "hello logger rinfo %d", 222222);
        l->log (LL_FATAL, "hello logger fatal %d", 222222);
        usleep (100*1000);
        l_main->trace ("hello logger +++++ %d", 333333);
        l_main->trace ("hello logger trace %d", 333333);
        l_main->debug ("hello logger debug %d", 333333);
        l_main->warning ("hello logger wning %d", 333333);
        l_main->rinfo ("hello logger rinfo %d", 333333);
        l_main->fatal ("hello logger fatal %d", 333333);

        LOG_TRACE(l_main, "hello logger string stream " << 444444);
        LOG_TRACE(l_main, "hello logger trace " << 444444);
        LOG_DEBUG(l_main, "hello logger debug " << 444444);
        LOG_WNING(l_main, "hello logger wning " << 444444);
        LOG_RINFO(l_main, "hello logger rinfo " << 444444);
        LOG_FATAL(l_main, "hello logger fatal " << 444444);
        usleep (100*1000);

        //mian.longstr属性继承自main模块(前面相同,那个 . 就表示继承关系-_-!)
        long_str->log (LL_TRACE, "long str [%s]", longstr);

        //mian.test.sub*属性继承自main.sub模块(前面相同,那个 . 就表示继承关系-_-!)
        l_sub1->log (LL_TRACE, "hello logger %d", 999);
        l_sub2->log (LL_FATAL, "hello logger %d", 999);
        l_sub3->log (LL_TRACE, "hello logger %d", 999);
        l_sub4->log (LL_TRACE, "hello logger %d", 999);
        l_sub5->log (LL_RINFO, "hello logger %d", 999);
        l_sub6->log (LL_WNING, "hello logger %d", 999);
        l_sub7->log (LL_TRACE, "hello logger %d", 999);
        l_sub8->log (LL_DEBUG, "hello logger %d", 999);
        l_sub9->log (LL_TRACE, "hello logger %d", 999);
        l_sub10->log (LL_RINFO, "hello logger %d", 999);
        l_sub11->log (LL_TRACE, "hello logger %d", 999);
        l_sub12->log (LL_TRACE, "hello logger %d", 999);

        //sleep(3);
        //为了方便看日志文件,只循环一次就跳出循环
        //break;

    }

    return 0;
}
